Ext.define('Admin.view.process.definition.ProcessDefinitionController', {
    extend: 'Ext.app.ViewController',
    alias: 'controller.processdefinition',

    onClickProcessDefinitionGridUploadButton: function() {
        Ext.create(Ext.apply({ xtype: 'processdefinition-upload-window' }));
    },

    onClickProcessDefinitionUploadFormCancelButton: function(btn) {
        var win = btn.up('window');
        if (win) {
            win.close();
        }
    },

    onClickProcessDefinitionUploadFormUploadButton: function(btn) {
        var form = btn.up('form');
        form.getForm().submit({
            url: 'definition/deploy',
            waitMsg: '正在上传，请耐心等待....',
            params: {
                "_csrf": document.getElementsByTagName('meta')['_csrf'].content
            },
            success: function(form, action) {
                Ext.Msg.alert('上传成功', action.result.msg, function() {
                    btn.up('window').close();
                });
                Ext.getCmp('process-definition-grid').getStore().reload();
            },
            failure: function(form, action) {
                Ext.Msg.alert('上传失败', action.result.msg);
            }
        });
    },

    onClickProcessDefinitionReadResourceButton: function(view, recIndex, cellIndex, item, e, record) {
        var url = 'definition/resource?pdid=' + record.get('id') + '&resourceName=' + record.get('resourceName');
        //window.open(url);
        var win = new Ext.window.Window({
            title: '查看流程文件bpmn',
            width: 800,
            height: 500,
            layout: 'fit',
            items: [{
                xtype: 'panel',
                id: 'read-resource-panel',
                autoScroll: true
            }]
        });

        win.show();

        Ext.Ajax.request({
            url: url,
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            success: function(response, options) {
                var panel = Ext.getCmp("read-resource-panel");
                panel.body.update("<xmp>" + response.responseText + "</xmp>"); //autoScroll: true
                //panel.body.update("<textarea style='width:775px;height:420px;overflow:auto;'>"+response.responseText+"</textarea>");
            },
            failure: function(response, options) {
                Ext.Msg.alert('错误', response.status + ' ' + response.statusText);
            }
        });
    },

    onClickProcessDefinitionReadDiagramResourceButton: function(view, recIndex, cellIndex, item, e, record) {
        var url = 'definition/resource?pdid=' + record.get('id') + '&resourceName=' + record.get('diagramResourceName');
        var win = new Ext.window.Window({
            title: '查看流程PNG',
            width: 860,
            height: 500,
            layout: 'fit',
            items: [new Ext.Panel({
                resizeTabs: true,
                autoScroll: true,
                html: '<iframe scrolling="auto" frameborder="0" width="100%" height="100%" src=' + url + '></iframe>'
            })]
        });

        win.show();
    },

    onClickProcessDefinitionGridDeleteButton: function(view, recIndex, cellIndex, item, e, record) {
        Ext.Ajax.request({
            url: 'definition/delete',
            method: 'post',
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            params: {
                deploymentId: record.get('deploymentId')
            },
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('系统提示', json.msg);
                    Ext.getCmp('process-definition-grid').getStore().reload();
                } else {
                    Ext.Msg.alert('系统提示', json.msg);
                }
            },
            failure: function(response, options) {
                var responseText = response.statusText;
                if (response.status == 401 || response.status == 403) {
                    responseText = '缺少权限';

                }
                Ext.Msg.alert('错误', response.status + ' ' + responseText);
            }
        });
    },

    onClickProcessDefinitionActiveButton: function(view, recIndex, cellIndex, item, e, record) {
        var url = 'definition/update/active/' + record.get('id');

        Ext.Ajax.request({
            url: url,
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('系统提示', json.msg);
                    Ext.getCmp('process-definition-grid').getStore().reload();
                } else {
                    Ext.Msg.alert('系统提示', json.msg);
                }
            },
            failure: function(response, options) {
                var responseText = response.statusText;

                if (response.status == 401 || response.status == 403) {
                    responseText = '缺少权限';
                }
                Ext.Msg.alert('错误', response.status + ' ' + responseText);
            }
        });
    },

    onClickProcessDefinitionSuspendButton: function(view, recIndex, cellIndex, item, e, record) {
        var url = 'definition/update/suspend/' + record.get('id');
        Ext.Ajax.request({
            url: url,
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('系统提示', json.msg);
                    Ext.getCmp('process-definition-grid').getStore().reload();
                } else {
                    Ext.Msg.alert('系统提示', json.msg);
                }
            },
            failure: function(response, options) {
                var responseText = response.statusText;

                if (response.status == 401 || response.status == 403) {
                    responseText = '缺少权限';
                }
                Ext.Msg.alert('错误', response.status + ' ' + responseText);
            }
        });
    },

    onClickProcessDefinitionGridConvertModelButton: function(view, recIndex, cellIndex, item, e, record) {
        var url = 'definition/convert-to-model/' + record.get('id');
        Ext.Ajax.request({
            url: url,
            defaultHeaders: {
                "X-CSRF-TOKEN": document.getElementsByTagName('meta')['_csrf'].content
            },
            success: function(response, options) {
                var json = Ext.util.JSON.decode(response.responseText);
                if (json.success) {
                    Ext.Msg.alert('系统提示', json.msg);
                    Ext.getCmp('process-definition-grid').getStore().reload();
                } else {
                    Ext.Msg.alert('系统提示', json.msg);
                }
            },
            failure: function(response, options) {
                var responseText = response.statusText;
                if (response.status == 401 || response.status == 403) {
                    responseText = '缺少权限';

                }
                Ext.Msg.alert('错误', response.status + ' ' + responseText);
            }
        });
    }
});